package org.tmad.ocms.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.tmad.ocms.log.LogFactory;
import org.tmad.ocms.service.ForgotPasswordService;

/**
 * Servlet implementation class ForgotPasswordServlet
 */
public class ForgotPasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LogFactory.getLogger();
	private ForgotPasswordService service = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForgotPasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unused")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//username
		//activationCode
		String userName = request.getParameter("email");
		String activationCode = request.getParameter("activationCode");
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher("/active.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		service = new ForgotPasswordService();
		String email = request.getParameter("email");
		@SuppressWarnings("unused")
		String password = request.getParameter("password");
		@SuppressWarnings("unused")
		String confirmpassword = request.getParameter("confirmpassword");
		try {
				service.sendPasswordDetails(email);
			} catch (Exception e) {
				logger.error("authentication failed for " + email, e);
				request.setAttribute("error", e.getMessage());
			}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher("/login.jsp");
		dispatcher.forward(request, response);
	}

}
